﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ProgNetComponentsDx.Forms
{
    public partial class ExceptionForm : XtraForm
    {
        public static bool DefaultShowDetails
        {
            get;
            set;
        }

        private ExceptionForm()
        {
            InitializeComponent();
        }

        public ExceptionForm(Exception ex)
        {
            InitializeComponent();
            this.Icon = SystemIcons.Error;

            if (ex != null)
            {
                txtMessage.EditValue = ex.Message;
                txtType.EditValue = ex.GetType().FullName;
                txtSource.EditValue = ex.Source;
                StringBuilder sb = new StringBuilder();
                ProgNetComponents.Logs.LoggerProperties p = new ProgNetComponents.Logs.LoggerProperties();
                p.AdditionalStackTraceMaxLines=0;
                p.LogAdditionalStackTrace = ProgNetComponents.Utils.DefaultBool.True;
                p.LogDate = ProgNetComponents.Utils.DefaultBool.False;
                p.LogExceptionMessage = ProgNetComponents.Utils.DefaultBool.True;
                p.LogExceptionName = ProgNetComponents.Utils.DefaultBool.True;
                p.LogExceptionProperties = ProgNetComponents.Utils.DefaultBool.True;
                p.LogInnerExceptios = ProgNetComponents.Utils.DefaultBool.True;
                p.LogLogType = ProgNetComponents.Utils.DefaultBool.False;
                p.LogStackTrace = ProgNetComponents.Utils.DefaultBool.True;
                p.LogTime = ProgNetComponents.Utils.DefaultBool.False;
                p.LogType = ProgNetComponents.Logs.LogType.Error;
                p.StackTraceMaxLines = 0;

                ProgNetComponents.Logs.Logger.AppendExceptionInfo(sb, p, ex);

                meFullInfo.Text = sb.ToString();
                meStackTrace.Text = ex.StackTrace.ReplaceSingleEOL();
                ShowDetails(DefaultShowDetails);
            }
        }

        public static void ShowDialog(Exception ex)
        {
            using (ExceptionForm f = new ExceptionForm(ex))
            {
                f.ShowDialog();
            }
        }

        private void btnDetails_Click(object sender, EventArgs e)
        {
            ShowDetails(!tabDetails.Visible);
        }
        private void ShowDetails(bool show)
        {
            this.tabDetails.Visible = show;
            btnDetails.Text = "Details " + (show ? "<<<" : ">>>");
            this.MaximizeBox = show;
            this.MinimizeBox = show;
            if (show)
            {
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
                this.MaximumSize = Size.Empty;
                this.Height = 400;
            }
            else
            {
                this.Height = 190;
                this.Width = 400;
                this.MaximumSize = new Size(this.Width, this.Height);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            }
        }

        private void propertyGridControl1_CustomPropertyDescriptors(object sender, DevExpress.XtraVerticalGrid.Events.CustomPropertyDescriptorsEventArgs e)
        {
            
        }
    }
}
